<?php

namespace App\Containers\AppSection\File\Tasks;

use App\Containers\AppSection\File\Data\Repositories\FileRepository;
use App\Containers\AppSection\File\Models\File;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Parents\Tasks\Task;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Facades\Storage;

class GetUrlFileTask extends Task
{
    const DAYS = 5;
    protected FileRepository $repository;
    protected Storage $storage;

    public function __construct(
        FileRepository $repository,
        Storage        $storage
    )
    {
        $this->storage = $storage;
        $this->repository = $repository;
    }

    public function run(int $id): string
    {
        try {
            /** @var File $file */
            $file = $this->repository->find($id);

            return $this->storage::disk(config('filesystems.cloud'))->temporaryUrl($file->path, (new Carbon())->addDays(self::DAYS));

        } catch (Exception $exception) {
            throw new NotFoundException();
        }
    }
}
